//imagePreview
function preview()  
{  
 document.getElementById('preview').style.display = 'none';  
   
        //验证上传文件格式是否正确   
     /*   var pos = picPath.lastIndexOf(".");   
        var lastname = picPath.substring(pos, picPath.length)   
        if (lastname.toLowerCase() != ".jpg") {   
            alert("您上传的文件类型为" + lastname + "，图片必须为 JPG 类型");   
            return false;   
        }   */
          
        //验证上传文件宽高比例   
        if (image.height / image.width > 1.5 || image.height / image.width < 1.25) {   
            alert("您上传的图片比例超出范围，宽高比应介于1.25-1.5");   
            return;   
        }  
           
        //验证上传文件是否超出了大小   
        if (image.fileSize / 1024 > 150) {   
            alert("您上传的文件大小超出了150K限制！");   
            return false;   
}  
        // 下面代码用来获得图片尺寸，这样才能在IE下正常显示图片  
        document.getElementById('box').innerHTML = "<img width='"+image.width+"' height='"+image.height+"' id='aPic' src='"+picPath+"'>";  
   
}  
// show view button  
    function buttonShow()  
    {  
         /* 
         这里用来解决图片加载延时造成的预览失败. 
         简单说明一下，当image对象的src属性发生改变时JavaScript会重新给image装载图片内容， 
         这通常是需要一些时间的，如果在加载完成之前想将图片显示出来就会造成错误，所以我们 
         通过图片的宽度和高度来判断图片是否已经被成功加载，加载完毕才会显示预览按钮. 
         这里我仍然有一个困惑，在IE7下预览效果偶尔会失效. 
         */  
         if ( image.width == 0 || image.height == 0 ) {  
            setTimeout(buttonShow, 1000);  
         } else {  
            document.getElementById('preview').style.display = 'block';  
         }  
    }  
    /*加载图片*/  
    function loadImage(ele) {  
         picPath = getPath(ele);  
         image = new Image();  
         image.src = picPath;  
         /*一秒钟后调用显示预览按钮函数*/  
         setTimeout(buttonShow, 1000);  
    }  
       
    //得到图片的完整路径  
    function getPath(obj)  
    {  
         if(obj)  
         {  
             //ie  
             if (window.navigator.userAgent.indexOf("MSIE")>=1)  
            {  
                obj.select();  
                // IE下取得图片的本地路径  
                return document.selection.createRange().text;  
             }  
             //firefox  
             else if(window.navigator.userAgent.indexOf("Firefox")>=1)  
             {  
                if(obj.files)  
                {  
                 // Firefox下取得的是图片的数据  
                return obj.files.item(0).getAsDataURL();  
                }  
                return obj.value;  
             }  
         return obj.value;  
         }  
    }  


// JavaScript Document
function goTopEx() {
	var obj = document.getElementById("goTopBtn");
	function getScrollTop() {
		return document.documentElement.scrollTop + document.body.scrollTop;
	}
	function setScrollTop(value) {
		if (document.documentElement.scrollTop) {
			document.documentElement.scrollTop = value;
		} else {
			document.body.scrollTop = value;
		}
	}
	window.onscroll = function() {
		getScrollTop() > 0 ? obj.style.display = ""
				: obj.style.display = "none";
	}
	obj.onclick = function() {
		var goTop = setInterval(scrollMove, 10);
		function scrollMove() {
			setScrollTop(getScrollTop() / 1.1);
			if (getScrollTop() < 1)
				clearInterval(goTop);
		}
	}
}
/*
function goTopEx() {
	var obj = document.getElementById("goTopBtn");
	function getScrollTop() {
		return document.documentElement.scrollTop;
	}
	function setScrollTop(value) {
		document.documentElement.scrollTop = value;
	}
	window.onscroll = function() {
		getScrollTop() > 0 ? obj.style.display = ""
				: obj.style.display = "none";
	}
	obj.onclick = function() {
		var goTop = setInterval(scrollMove, 10);
		function scrollMove() {
			setScrollTop(getScrollTop() / 1.1);
			if (getScrollTop() < 1)
				clearInterval(goTop);
		}
	}
}
*/
var to, width, height, position, autohide, opacity;

function notifit_setDefaultValues() {
	// Default size
	width = 400;
	height = 60;
	// Default position
	position = "right";
	// Default autohide
	autohide = true;
	// Default msg
	msg = "";
	// Default opacity (Only Chrome, Firefox and Safari)
	opacity = 1;
}
function notif(config) {

	notifit_setDefaultValues();

	if (config.position) {
		if (config.position == "center" || config.position == "left"
				|| config.position == "right") {
			position = config.position; // Take the position
		}
	}
	if (config.width) {
		if (config.width > 0) {
			width = config.width; // Take the width in pixels
		} else if (config.width === "all") {
			width = screen.width - 60; // Margin difference
		}
	}
	if (config.height) {
		if (config.height < 100 && config.height > 0) {
			height = config.height; // Take the height in pixels
		}
	}
	if (typeof config.autohide !== "undefined")
		autohide = config.autohide;
	var div = "<div id='ui_notifIt'><p>" + ((config.msg) ? config.msg : "")
			+ "</p></div>";

	$("#ui_notifIt").remove(); // Preventive remove
	clearInterval(to); // Preventive clearInterval
	$("body").append(div);
	$("#ui_notifIt").css("height", height);
	$("#ui_notifIt").css("width", width);

	switch (position) {
	case "center":
		$("#ui_notifIt").css("top", parseInt(0 - (height + 10)));
		break;
	case "right":
		$("#ui_notifIt").css("right", parseInt(0 - (width + 10)));
		break;
	case "left":
		$("#ui_notifIt").css("left", parseInt(0 - (width + 10)));
		break;
	default:
		$("#ui_notifIt").css("right", parseInt(0 - (width + 10)));
		break;
	}

	if (config.opacity) {
		$("#ui_notifIt").css("opacity", config.opacity);
	}

	switch (config.type) {
	case "error":
		$("#ui_notifIt").addClass("error");
		break;
	case "success":
		$("#ui_notifIt").addClass("success");
		break;
	case "info":
		$("#ui_notifIt").addClass("info");
		break;
	case "warning":
		$("#ui_notifIt").addClass("warning");
		break;
	default:
		$("#ui_notifIt").addClass("default");
		break;
	}

	switch (position) {
	case "left":
		$("#ui_notifIt").css("left", parseInt(0 - (width * 2)));
		break;
	case "right":
		$("#ui_notifIt").css("right", parseInt(0 - (width * 2)));
		break;
	case "center":
		var mid = window.innerWidth / 2;
		$("#ui_notifIt").css("left", mid - parseInt(width / 2));
		break;
	default:
		var mid = window.innerWidth / 2;
		$("#ui_notifIt").css("left", mid - parseInt(width / 2));
		break;
	}
	$("#ui_notifIt p").css("line-height", height + "px");

	switch (position) {
	case "center":
		$("#ui_notifIt").animate({
			top : 10
		});
		break;
	case "right":
		$("#ui_notifIt").animate({
			right : 10
		});
		break;
	case "left":
		$("#ui_notifIt").animate({
			left : 10
		});
		break;
	default:
		$("#ui_notifIt").animate({
			right : 10
		});
		break;
	}

	$("#ui_notifIt").click(function() {
		notifit_dismiss();
	});

	if (autohide == true)
		to = setTimeout(function() {
			notifit_dismiss();
		}, 5000);

}

function notifit_dismiss() {
	clearInterval(to);
	if (position == "center") {
		$("#ui_notifIt").animate({
			top : parseInt(height - (height / 2))
		}, 100, function() {
			$("#ui_notifIt").animate({
				top : parseInt(0 - (height * 2))
			}, 100, function() {
				$("#ui_notifIt").remove();
			});
		});
	} else if (position == "right") {
		$("#ui_notifIt").animate({
			right : parseFloat(width - (width * 0.9))
		}, 100, function() {
			$("#ui_notifIt").animate({
				right : parseInt(0 - (width * 2))
			}, 100, function() {
				$("#ui_notifIt").remove();
			});
		});
	} else if (position == "left") {
		$("#ui_notifIt").animate({
			left : parseFloat(width - (width * 0.9))
		}, 100, function() {
			$("#ui_notifIt").animate({
				left : parseInt(0 - (width * 2))
			}, 100, function() {
				$("#ui_notifIt").remove();
			});
		});
	}
	notifit_setDefaultValues();
}
